package com.zzyq.pc.common.plugin.shiro.session;

import java.io.Serializable;
import java.util.Collection;

import org.apache.shiro.session.Session;
import org.apache.shiro.session.UnknownSessionException;
import org.apache.shiro.session.mgt.eis.AbstractSessionDAO;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * 
 * <p>
 * 
 * @Title: CustomShiroSessionDao.java
 * @Description: Session操作
 * @author: Sywd丶吴迪
 * @date: 2017年8月29日 下午4:21:19
 * @version V1.0
 *          <p>
 */
public class CustomShiroSessionDao extends AbstractSessionDAO {
    /**
     * Session操作对象
     */
    @Autowired
    private IShiroSessionRepository shiroSessionRepository;

    @Override
    public void update(Session session) throws UnknownSessionException {
        shiroSessionRepository.saveSession(session);
    }

    @Override
    public void delete(Session session) {
        if (session == null) {
            return;
        }
        Serializable id = session.getId();
        if (id != null) {
            shiroSessionRepository.deleteSession(id);
        }
    }

    @Override
    public Collection<Session> getActiveSessions() {
        return shiroSessionRepository.getAllSessions();
    }

    @Override
    protected Serializable doCreate(Session session) {
        Serializable sessionId = this.generateSessionId(session);
        this.assignSessionId(session, sessionId);
        shiroSessionRepository.saveSession(session);
        return sessionId;
    }

    @Override
    protected Session doReadSession(Serializable sessionId) {
        return shiroSessionRepository.getSession(sessionId);
    }

}
